package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuPrice;
import com.spzx.product.api.domain.SkuQuery;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "商品管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    IProductService productService;

    /**
     * 分页查询
     *
     * @param product
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> productList = productService.selectList(product);
        return getDataTable(productList);
    }

    /**
     * 新增商品
     *
     * @param product
     * @return
     */
    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult addProduct(@RequestBody Product product) {
        return success(productService.insertProduct(product));
    }

    /**
     * 查询商品详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询商品详情")
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return success(productService.selectProductById(id));
    }

    /**
     * 修改商品信息
     *
     * @param product
     * @return
     */
    @Operation(summary = "修改商品信息")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品信息
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除商品信息")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") List<Long> ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    /**
     * 商品审核
     *
     * @param id
     * @param audiStatus
     * @return
     */
    @Operation(summary = "商品审核")
    @GetMapping("/updateAudiStatus/{id}/{audiStatus}")
    public AjaxResult editAudiStatus(@PathVariable Long id, @PathVariable Integer audiStatus) {
        productService.updateAudiStatus(id, audiStatus);
        return success();
    }

    /**
     * 修改上下架状态
     *
     * @param id
     * @param status
     * @return
     */
    @Operation(summary = "上下架状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult editStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return success();
    }


    //==============================给前端调用的===========================================

    @InnerAuth //内部调用的注解
    @Operation(summary = "获取销量好的SKU")
    @GetMapping("/getTopSale")
    public R<List<ProductSku>> getTopSale() {
        return R.ok(productService.getTopSale());
    }


    /**
     * 商品列表查询
     *
     * @param pageNum
     * @param pageSize
     * @param skuQuery
     * @return
     */
    @InnerAuth
    @Operation(summary = "商品列表查询")
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable Integer pageNum,
            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @PathVariable Integer pageSize,
            @Parameter(name = "productQuery", description = "查询对象", required = false)
            SkuQuery skuQuery
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> productSkuList
                = productService.selectProductSkuList(skuQuery);
        return R.ok(getDataTable(productSkuList));
    }

    /**
     *获取商品sku
     * @param skuId
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品sku信息")
    @GetMapping("/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId){
        return R.ok(productService.getProductSku(skuId));
    }

    /**
     * 获取商品信息
     * @param id
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品信息")
    @GetMapping("/getProduct/{id}")
    public R<Product> getProduct(@PathVariable("id") Long id){
        return R.ok(productService.getProduct(id));
    }

    /**
     * 获取商品sku最新价格信息
     * @param skuId
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品sku最新价格信息")
    @GetMapping("/getSkuPrice/{skuId}")
    public R<SkuPrice> getSkuPrice(@PathVariable("skuId") Long skuId){
        return R.ok(productService.getSkuPrice(skuId));
    }

    /**
     * 获取商品详细信息
     * @param id
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品详细信息")
    @GetMapping("/getProductDetails/{id}")
    public R<ProductDetails> getProductDetails(@PathVariable("id") Long id){
        return R.ok(productService.getProductDetails(id));
    }

    /**
     * 获取商品sku规则详细信息
     * @param id
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品sku规则详细信息")
    @GetMapping("/getSkuSpecValue/{id}")
    public R<Map<String,Long>> getSkuSpecValue(@PathVariable("id") Long id){
        return  R.ok(productService.getSkuSpecValue(id));
    }

    /**
     * 获取商品sku库存信息
     * @param skuId
     * @return
     */
    @InnerAuth
    @Operation(summary = "获取商品sku库存信息")
    @GetMapping("/getSkuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId){
        return R.ok(productService.getSkuStock(skuId));
    }
}
